//
//  ScrollViewViewController.m
//  享动
//
//  Created by 沈浩hh on 16/4/8.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "ScrollViewViewController.h"

@interface ScrollViewViewController ()<UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *activity;
// 没有网络的提示框
@property (nonatomic, strong)  UIView *hint;
@end

@implementation ScrollViewViewController

- (void)loadData
{
    NSDictionary *parDic = @{@"article_id":_article_id};
    [RequestManager requestWithUrl:@"http://media.api.thejoyrun.com/article-get" requestType:RequestTypePost parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = dic[@"data"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
          
            
            _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, KWIDTH, KHEIGHT - 49)];
//            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataDic[@"article_url"]]];
//            [_webView loadRequest:request];
            NSData *html = [NSData dataWithContentsOfURL:[NSURL URLWithString:dataDic[@"article_url"]]];
            
            NSString *string = [[NSString alloc]initWithData:html encoding:NSUTF8StringEncoding];
            NSLog(@"%@", string);
            NSRange range1 = [string rangeOfString:@"<div class=\"read_collect\">阅读&nbsp"];
            NSRange rang2 = [string rangeOfString:@"var ua = navigator.userAgent"];
            NSInteger a = range1.location;
            NSInteger b = rang2.location - range1.location - 33;
            NSRange rang3 = NSMakeRange(a, b);
            NSString *stringTemp1 = @"";
            NSString *stringTemp =[string stringByReplacingCharactersInRange:rang3 withString:stringTemp1];
//            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"悦跑" withString:@"享动"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友来稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友投稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文来自悦跑圈跑友投稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈原创稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈独家约稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈专栏作者约稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈专栏作者稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];

            stringTemp = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",KWIDTH - 15,stringTemp];
            [_webView loadHTMLString:stringTemp baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];

            _webView.delegate = self;
            _webView.backgroundColor = [UIColor clearColor];
            [self.view addSubview:_webView];

            
        });
        
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    if ([[NetWorkState shareInstance]reachability]) {
        [self loadData];
    } else {
        [self addHint];
    }
    
    _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [_activity setCenter:CGPointMake(KWIDTH / 2, KHEIGHT / 2)];
    [_activity startAnimating];
    [_webView addSubview:_activity];
}

#pragma mark ---- 没有网络的提示框 ----
- (void)addHint
{
    // 没有网络的提示框
    _hint = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH / 4, KHEIGHT / 2 - KHEIGHT / 15, KWIDTH / 2, KHEIGHT / 15 * 2)];
    _hint.backgroundColor = [UIColor blackColor];
    _hint.alpha = 0.7;
    _hint.layer.cornerRadius = 10;
    [self.view addSubview:_hint];
    
    UIImageView *noNetworkimageView = [[UIImageView alloc]initWithFrame:CGRectMake(_hint.frame.size.width / 3 + 3, 0, _hint.frame.size.width / 3 + 3, _hint.frame.size.width / 3 * 53 / 63)];
    noNetworkimageView.image = [UIImage imageNamed:@"noNetwork"];
    [_hint addSubview:noNetworkimageView];
    
    UILabel *hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _hint.frame.size.height / 2, KWIDTH / 2, _hint.frame.size.height / 2)];
    hintLabel.text = @"网络超时";
    hintLabel.textAlignment = NSTextAlignmentCenter;
    hintLabel.textColor = [UIColor whiteColor];
    [_hint addSubview:hintLabel];
    
    [self performSelector:@selector(removeHint) withObject:nil afterDelay:3];
    
}

- (void)removeHint
{
    [_hint removeFromSuperview];
}

// webView的代理方法 加载结束后
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activity stopAnimating];
    
}

// 加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    [self addHint];
}
- (void)leftBarButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
